// This sample lists the viewer's friends (displaying the name of each one)
// Up to 20 friends are listed.

function init() {
   var req = opensocial.newDataRequest();
   var opt_params = {};
   opt_params[opensocial.DataRequest.PeopleRequestFields.MAX] = 20;
   var idspec = opensocial.newIdSpec({ "userId" : "VIEWER", 
                                       "groupId" : "FRIENDS" });
   req.add(req.newFetchPeopleRequest(idspec, 
      opt_params), 'viewerFriends');

   req.send(onLoadFriends);
   debug.say("Waiting for reply...");
}

function onLoadFriends(data) {
  if (data.hadError()) {
     debug.say("*** Error.");
     // ... more error handling should go here ...
     return;
  }

  var friends = data.get('viewerFriends').getData().asArray();
  debug.say(friends.length + " friends returned:");
  for (var i in friends)
    if (friends[i].getId()) debug.say('- ' + friends[i].getDisplayName());
}

gadgets.util.registerOnLoadHandler(init);

